#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>


using std::cin;
using std::cout;
using std::endl;
using std::vector;

const int inf = 1e9;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0): x(x), y(y) {}

    friend std::istream& operator>>(std::istream& lin, Point& point) {
        lin >> point.x >> point.y;
        return lin;
    }
};

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
    int n, m;
    cin >> n >> m;
    Point start, finish;
    cin >> start >> finish;

    vector <vector <int>> dist(n + 2);
    vector <vector <int>> walls(n + 2);
    vector <vector <bool>> was(n + 2);
    for (int i = 0; i < n + 2; ++i) {
        dist[i].resize(m + 2, inf);
        walls[i].resize(m + 2, 1);
        was[i].resize(m + 2, 0);
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> walls[i][j];
        }
    }
    dist[start.x][start.y] = 0;
    was[start.x][start.y] = 1;
    std::queue <Point> q;
    q.push(start);
    while (!q.empty()) {
        auto current = q.front();
        q.pop();
        for (int i = 0; i < 4; ++i) {
            Point to(current.x + dx[i], current.y + dy[i]);
            if (!walls[to.x][to.y] && !was[to.x][to.y]) {
                q.push(to);
                was[to.x][to.y] = true;
                dist[to.x][to.y] = dist[current.x][current.y] + 1;
            }
        }
    }
    if (dist[finish.x][finish.y] != inf) 
        cout << dist[finish.x][finish.y] << endl;
    else 
        cout << "NO\n";
}
